﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net.Sockets;
using System.Threading;
using System.Net;

using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using DIMP_Libraries;

namespace DIMP_serveur
{
    public class Serveur : DIMP_Libraries.Serveur
    {
        public Serveur(Int32 port) : base(port)
        {
            onClientConnected += new EventHandler(Serveur_onClientConnected);
        }

        /*
        * Quand un client c'est connecté, lui souhaite la bienvenue
        */
        void Serveur_onClientConnected(object sender, EventArgs e)
        {
            ClientServeur client = (ClientServeur)sender;
            ActionMessage unMsg = new ActionMessage();
            unMsg.Type = ActionType.Welcome;
            client.SendMessage(unMsg);
        }

        /*
         * Quand un client a envoyé un message
         */
        public override void clientSrv_onMessageReceived(object sender, MessageEventArg e)
        {
            DIMP_Libraries.IMessage msg = (DIMP_Libraries.IMessage)e.Message;
            Console.WriteLine("Message de type : " + msg.GetType().FullName);

            if (msg.GetType() == typeof(SendTextMessage))
            {
                SendTextMessage message = new SendTextMessage();
                message.Texte = "["+e.Client.Pseudo+"] : " + ((SendTextMessage)msg).Texte;
                foreach (var item in _clients)
                {
                    item.SendMessage(message);
                }
            }

            if (msg.GetType() == typeof(DefinePseudoMessage))
            {
                DefinePseudoMessage message = (DefinePseudoMessage)msg;
                ActionMessage unMsg = null;
                if (message.Pseudo == "")
                {
                    unMsg = new ActionMessage();
                    unMsg.Type = ActionType.LoginFail;

                    e.Client.SendMessage(unMsg);
                }
                else
                {
                    foreach (var item in this._clients)
                    {
                        if (item.Pseudo == message.Pseudo)
                        {
                            unMsg = new ActionMessage();
                            unMsg.Type = ActionType.LoginFail;

                            e.Client.SendMessage(unMsg);
                            break;
                        }
                    }
                    if (unMsg == null)
                    {
                        unMsg = new ActionMessage();
                        unMsg.Type = ActionType.LoginOK;
                        e.Client.Pseudo = message.Pseudo;
                        e.Client.SendMessage(unMsg);
                    }
                }
            }

            if (msg.GetType() == typeof(ActionMessage))
            {
                ActionMessage message = (ActionMessage)msg;
                switch (message.Type)
                {
                    case ActionType.Welcome:
                        break;
                    case ActionType.ServerOff:
                        break;
                    case ActionType.LoginOK:
                        break;
                    case ActionType.LoginFail:
                        break;
                    default:
                        break;
                }
            }
        }

    }
}
